<template>
  <el-breadcrumb
    class="app-breadcrumb"
    separator="/"
  >
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
        v-for="(item,index) in levelList"
        :key="item.path"
      >
        <span
          v-if="index == 0"
          class="no-redirect"
        >{{ item.title }}</span>
        <a
          v-else
          @click.prevent="handleLink(item)"
        >{{ item.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import pathToRegexp from 'path-to-regexp'
import { EventBus } from '@/eventbus/EventBus'
import { mapGetters } from 'vuex'
import i18n from '@/lang/i18n'

export default {
  data() {
    return {
      levelList: null
    }
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    },
    menus() {
      this.getBreadcrumb();
    }
  },
  computed: {
    ...mapGetters([
      'menus',
    ]),
  },
  filters: {
    i18n(str) {
      return i18n.t(str);
    }
  },
  created() {
    this.getBreadcrumb();
  },
  mounted() {
    EventBus.$on('language-changed', () => {
      this.getBreadcrumb();
    })
  },
  beforeDestroy() {
    EventBus.$off('language-changed');
  },
  methods: {
    getBreadcrumb() {
      if(this.$route.path === '/home') {
        let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
        /* const first = matched[0]

        if (!this.isDashboard(first)) {
          matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
        } */

        this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
        this.levelList.forEach(l => l.title = l.meta.title)
        // console.log(this.levelList)
        return;
      }
      // only show routes with meta.title
      // console.log(this.menus);
      let mapRoutes = []
      let pureMenus = this.menus.slice(2, this.menus.length - 1);
      pureMenus.forEach(parent => {
        if (parent.path === '/' || !parent.children) {
          return;
        }
        parent.children.forEach(child => {
          mapRoutes.push([{
            path: parent.path,
            title: parent.meta?.title
          },{
            path: parent.path+"/"+child.path,
            title: child.meta?.title
          }])
        })

      })
      // console.log(mapRoutes)
      this.levelList = mapRoutes.find(match => match[1].path === this.$route.path);
      // console.log(this.levelList)

    },
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
    },
    pathCompile(path) {
      // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
      const { params } = this.$route
      var toPath = pathToRegexp.compile(path)
      return toPath(params)
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
        return
      }
      this.$router.push(path)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;
  background-color: #63945E;

  .no-redirect {
    color: #000;
    cursor: text;
  }
}
</style>
